Skip to content

[Feat] 벡터 + 키워드 하이브리드 검색 구현#30

Merged
1024andrew merged 9 commits intodevfrom
feat/#28
May 4, 2026
Merged

[Feat] 벡터 + 키워드 하이브리드 검색 구현#30
1024andrew merged 9 commits intodevfrom
feat/#28

Conversation

@kimssirr
Copy link
Copy Markdown
Contributor

@kimssirr kimssirr commented May 3, 2026

유형

  • Feat
  • Fix
  • Design
  • Docs
  • Chore
  • Hotfix

작업 내용

하이브리드 검색에서 벡터 검색과 키워드 검색을 더 안정적으로 결합하도록 개선했습니다.
키워드 점수가 없을 때 최종 유사도가 낮아지던 문제를 수정하고, fallback 판단은 하이브리드 점수가 아니라 벡터 점수 기준으로 동작하도록 변경했습니다.

또한 검색용으로 확장된 retrieval_query가 벡터 임베딩과 키워드 검색 양쪽에 동일하게 사용되도록 수정했습니다.

변경 사항 (있다면)

  • 하이브리드 점수 계산식을 키워드 보너스 방식으로 변경
  • 키워드 매칭이 없으면 similarityvector_score 그대로 유지되도록 수정
  • 키워드 매칭이 있으면 vector_score + keyword_weight * keyword_score * (1 - vector_score) 방식으로 가산
  • vector_score를 추가하고 fallback threshold 판단 기준으로 사용
  • similarity는 최종 하이브리드 점수로 저장
  • 확장된 retrieval_query를 벡터 임베딩과 키워드 검색 모두에 전달
  • query expansion fallback 경로도 하이브리드 검색을 사용하도록 통일
  • 답변 생성 결과에서 사용한 참고 정보 번호를 받아 해당 chunk를 출처로 선택하도록 개선
  • 관련 테스트 추가 및 갱신

리뷰 포인트

  • 키워드 점수가 없을 때 벡터 유사도가 낮아지지 않는지
  • similarityvector_score의 역할이 명확히 분리되어 있는지
  • fallback 판단이 vector_score 기준으로 동작하는지
  • 확장된 retrieval_query가 벡터 검색과 키워드 검색에 동일하게 전달되는지
  • query expansion fallback에서도 하이브리드 검색이 사용되는지
  • 답변 출처 chunk 선택이 실제 사용한 참고 정보 기준으로 동작하는지

테스트

  • 로컬 실행 확인
  • /docs 수동 확인
  • pytest 실행

스크린샷

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request implements a hybrid search mechanism and query expansion logic to enhance the RAG-based chat service. Key updates include new repository methods for combined vector and keyword searching, an LLM-driven query rewriting service, and a transition to structured JSON output for the answer generator to improve source tracking. The review feedback identifies potential runtime errors caused by premature database session closure, performance bottlenecks in the SQL-based keyword search, and opportunities to reduce redundant LLM API calls during query expansion.

Comment thread app/services/chat_service.py Outdated
Comment thread app/services/chat_service.py Outdated
Comment on lines +395 to +401
to_tsvector(
'simple',
COALESCE(rc.chunk_text, '') || ' ' ||
COALESCE(rd.content, '') || ' ' ||
COALESCE(rc.keywords::text, '')
),
websearch_to_tsquery('simple', :query_text)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

SQL 쿼리 내에서 to_tsvector를 검색 시마다 매번 호출하는 방식은 성능상 큰 병목이 될 수 있습니다. 특히 현재 쿼리에서는 SELECT, ORDER BY, WHERE 절에서 반복적으로 계산되고 있어 데이터량이 많아질 경우 인덱스를 활용하지 못하고 검색 속도가 급격히 저하됩니다.

개선 제안:

  1. regulation_chunk 테이블에 tsvector 타입의 컬럼을 추가하고, 데이터 삽입/수정 시 미리 계산하여 저장하십시오.
  2. 해당 컬럼에 GIN 인덱스를 생성하여 검색 성능을 최적화하십시오.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분은 수정해 주신건가요?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

새 작업이 필요하다 생각해 새 이슈로 팠습니다! 이 브랜치 머지 후 작업하려구요

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

알겠습니다!

Comment thread app/services/chat_service.py
Comment on lines +430 to +433
expanded_query = expand_query_for_retrieval(
question=question,
dormitory=None,
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

위의 단일 생활관 로직과 마찬가지로, 377행에서 이미 쿼리 확장이 일어났을 가능성이 있습니다. 중복된 LLM 호출을 방지하기 위해 rewritten_query 상태를 체크하십시오.

Suggested change
expanded_query = expand_query_for_retrieval(
question=question,
dormitory=None,
)
if _is_no_answer(answer_result.answer):
expanded_query = rewritten_query if rewritten_query != question else expand_query_for_retrieval(
question=question,
dormitory=None,
)

@kimssirr kimssirr changed the title Feat/#28 [Feat] 벡터 + 키워드 하이브리드 검색 구현 May 3, 2026
@kimssirr kimssirr self-assigned this May 3, 2026
@kimssirr kimssirr requested a review from 1024andrew May 3, 2026 13:08
@kimssirr
Copy link
Copy Markdown
Contributor Author

kimssirr commented May 3, 2026

SQL 쿼리 내에서 to_tsvector를 검색 시마다 매번 호출하는 방식은 새 이슈로 따로 뺐습니다.

@1024andrew
Copy link
Copy Markdown
Contributor

사라님, 충돌이 있어서 머지가 안된다고 하는데 한 번 봐주실 수 있나요

@kimssirr kimssirr added the feat 구현·개선 사항에 관련된 내용입니다. label May 4, 2026
@1024andrew 1024andrew merged commit fdc115b into dev May 4, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feat 구현·개선 사항에 관련된 내용입니다.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants